package com.benny_driving.act.frg;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.benny_driving.R;
import com.benny_driving.act.InAppAct;
import com.benny_driving.act.LoginAct;
import com.benny_driving.act.NoAct;
import com.benny_driving.act.PerAccAct;
import com.benny_driving.act.PerInfoAct;
import com.benny_driving.act.SotEvaAct;
import com.benny_driving.constant.LoginConstant;

public class ChangeFrg extends Fragment implements View.OnClickListener {
	private Intent i = new Intent();
	private ViewGroup vg;
	private ViewFlipper flipper;
	private ImageView previous, next;
	private ImageView info, acc, inApp, phn400, rem, share, score;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View fragmentView = inflater.inflate(R.layout.fm_change, container,
				false);
		this.vg = container;
		initView(fragmentView);
		return fragmentView;
	}

	private void initView(View fragmentView) {
		flipper = (ViewFlipper) fragmentView.findViewById(R.id.vf_Flipper);
		previous = (ImageView) fragmentView.findViewById(R.id.iv_Previous);
		next = (ImageView) fragmentView.findViewById(R.id.iv_Next);
		flipper.addView(addLeftLayoutById(R.layout.vf_left));
		flipper.addView(addCenLayoutById(R.layout.vf_center));
		flipper.addView(addRightLayoutById(R.layout.vf_right));
		flipper.setDisplayedChild(1);
		previous.setOnClickListener(this);
		next.setOnClickListener(this);
	}

	private View addLeftLayoutById(int res) {
		View view = View
				.inflate(getActivity().getApplicationContext(), res, vg);
		info = (ImageView) view.findViewById(R.id.Vf_iv_PerInfo);
		acc = (ImageView) view.findViewById(R.id.Vf_iv_PerAcc);
		inApp = (ImageView) view.findViewById(R.id.Vf_iv_InApp);
		info.setOnClickListener(new intentOcl());
		acc.setOnClickListener(new intentOcl());
		inApp.setOnClickListener(new intentOcl());
		return view;
	}

	private View addCenLayoutById(int res) {
		View view = View
				.inflate(getActivity().getApplicationContext(), res, vg);
		phn400 = (ImageView) view.findViewById(R.id.Vf_iv_Phn400);
		phn400.setOnClickListener(new intentOcl());
		return view;
	}

	private View addRightLayoutById(int res) {
		View view = View
				.inflate(getActivity().getApplicationContext(), res, vg);
		rem = (ImageView) view.findViewById(R.id.Vf_iv_Rem);
		share = (ImageView) view.findViewById(R.id.Vf_iv_Share);
		score = (ImageView) view.findViewById(R.id.Vf_iv_Score);
		rem.setOnClickListener(new intentOcl());
		share.setOnClickListener(new intentOcl());
		score.setOnClickListener(new intentOcl());
		return view;
	}

	private class intentOcl implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			if (v.equals(info)) {
				if (LoginConstant.LOGIN_STATE) {
					i.setClass(getActivity().getApplicationContext(),
							PerInfoAct.class);
				} else {
					Class<PerInfoAct> context = PerInfoAct.class;
					i.putExtra("class", context);
					i.setClass(getActivity().getApplicationContext(),
							LoginAct.class);
				}
			}
			if (v.equals(acc)) {
				if (LoginConstant.LOGIN_STATE) {
					i.setClass(getActivity().getApplicationContext(),
							PerAccAct.class);
				} else {
					Class<PerAccAct> context = PerAccAct.class;
					i.putExtra("class", context);
					i.setClass(getActivity().getApplicationContext(),
							LoginAct.class);
				}
			}
			if (v.equals(inApp)) {
				if (LoginConstant.LOGIN_STATE) {
					i.setClass(getActivity().getApplicationContext(),
							InAppAct.class);
				} else {
					Class<InAppAct> context = InAppAct.class;
					i.putExtra("class", context);
					i.setClass(getActivity().getApplicationContext(),
							LoginAct.class);
				}
			}
			if (v.equals(phn400)) {
				i = new Intent();
				i.setAction(Intent.ACTION_DIAL);
				i.setData(Uri.parse("tel:" + "4008200972"));
			}
			if (v.equals(rem)) {
				i.setClass(getActivity().getApplicationContext(), NoAct.class);
			}
			if (v.equals(share)) {
				i.setClass(getActivity().getApplicationContext(), NoAct.class);
			}
			if (v.equals(score)) {
				i.setClass(getActivity().getApplicationContext(),
						SotEvaAct.class);
			}
			i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(i);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(previous)) {
			if (flipper.getDisplayedChild() != 0) {
				flipper.setInAnimation(AnimationUtils.makeInAnimation(
						getActivity(), true));
				flipper.setDisplayedChild(0);
				next.setVisibility(View.GONE);
				previous.setVisibility(View.VISIBLE);
			} else {
				flipper.setInAnimation(AnimationUtils.makeInAnimation(
						getActivity(), false));
				flipper.setDisplayedChild(1);
				next.setVisibility(View.VISIBLE);
			}
		} else if (v.equals(next)) {
			if (flipper.getDisplayedChild() != flipper.getChildCount() - 1) {
				flipper.setInAnimation(AnimationUtils.makeInAnimation(
						getActivity(), false));
				flipper.setDisplayedChild(2);
				previous.setVisibility(View.GONE);
				next.setVisibility(View.VISIBLE);
			} else {
				flipper.setInAnimation(AnimationUtils.makeInAnimation(
						getActivity(), true));
				flipper.setDisplayedChild(1);
				previous.setVisibility(View.VISIBLE);
			}
		}
	}
}
